package com.example.linyiquan.application.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.linyiquan.application.R;
import com.example.linyiquan.application.util.MyVolley;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by linyiquan on 17-4-20.
 */

public class RegisterActivity extends Activity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private EditText et_password1;
    private Button bt_register;

    private String username;
    private String password;
    private String password1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initView();
    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);
        et_password1 = (EditText) findViewById(R.id.password1);
        bt_register = (Button) findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        gethttp();
    }

    private void gethttp() {
        MyVolley.init(this);

        username = et_username.getText().toString();
        password = et_password.getText().toString();
        password1 = et_password1.getText().toString();

        String url = "http://120.76.96.34/weizhanjsp/user/register?sex=0&city=厦门";
        url += "&username=" + username;
        url += "&password=" + password;
        url += "&birthday=" + System.currentTimeMillis();
        url += "&clientType=" + 1;

        MyVolley.get(url, null, new MyVolley.OnResultLitener() {
            @Override
            public void done(JSONObject result) {

                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(password1)) {
                    Toast.makeText(RegisterActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!password.equals(password1)) {
                    Toast.makeText(RegisterActivity.this, "验证密码不匹配", Toast.LENGTH_SHORT).show();
                    return;
                }

                int resultCode;
                try {
                    resultCode = result.getInt("resultCode");
                    if (resultCode == 0) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void fail(VolleyError error) {
                Toast.makeText(RegisterActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }

}
